const url = "https://www.ifeng.com/"

const puppeteer=require('puppeteer')


const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time);
}) 



//自运行函数
;(async ()=>{


    console.log("开始")

     //声明一个browser相当于浏览器
     const browser = await puppeteer.launch({
        args: ['--no-sandbox'],
        dumpio: false
    })

    const page = await browser.newPage()  //开启一个新页面
    await page.goto(url, {
        waitUntil: 'networkidle2'  //当这个网页空闲的时候
    })

    await sleep(1000)  //等待两秒


    const result = await page.evaluate(() => {
        var items=document.querySelectorAll('#newsList li a') //NodeList
         
        var itemAry = []; //NodeList转化为数组
        for(var i=0, len = items.length; i < len; i++){     
            itemAry.push(items[i]);    
        } 

        var links = []

        if (itemAry.length >= 1) {


            for (let index = 0; index < itemAry.length; index++) {
                const title = itemAry[index].innerHTML;
                const hreflink = itemAry[index].href;
                //https://www.ifeng.com/c/  正则判断是否含有该字符串的链接再保存
                if (hreflink.search('https://www.ifeng.com/c/')&&hreflink.split("/c/")[1]) {
                    var herfId = hreflink.split("/c/")[1]
 
                    links.push({
                        title,
                        hreflink,
                        herfId
                    })     
         
                }
 
            }
 
        }

        return links
 
    })


    for (let i = 0; i < result.length; i++) {

        await page.goto(result[i].hreflink, {
            waitUntil: 'networkidle2'
        })

        await  sleep(1000)

        detail= await page.evaluate(() => {

            try {
                var detailTitle=document.querySelector('.topic-_XJ6ViSR').innerHTML
            } catch (error) {
                var detailTitle='未获取到标题'
            }

            try {
                var detailDate=document.querySelector('.date-3fxruanH').innerHTML
            } catch (error) {
                var detailDate='未获取到日期'
            }

            try {
                 var detailFrom=document.querySelector('.source-1Pn6hUH2 a').innerHTML
            } catch (error) {
                var detailFrom='未获取到来源'
            }

            try {
               var detailContent=document.querySelector('.text-3zQ3cZD4').innerHTML 
            } catch (error) {
                var detailContent='未获取到内容'
            }
               
                return {
                    detailTitle,detailDate,detailFrom,detailContent
                }
        })       

        result[i].detail= detail
    }

    browser.close()
    process.send({ result })
    process.exit(0)

})()





